<?php

/**
 * @filename common.php 
 * @encoding UTF-8 
 * @author nemo.xiaolan <a href="mailto:335454250@qq.com">335454250@qq.com</a>
 * @link <a href="http://www.sep-v.com">http://www.sep-v.com</a>
 * @license http://www.sep-v.com/code-license
 * @datetime 2013-11-17 10:35:33
 * @description
 * 
 */

function makeBillCode() {
    $year_code = array('A','B','C','D','E','F','G','H','I','J');
    $order_sn = $year_code[intval(date('Y'))-2010].
    strtoupper(dechex(date('m'))).date('d').
    substr(time(),-5).substr(microtime(),2,5).sprintf('%d',rand(0,99));
    return $order_sn;
}

function getUserCache() {
    $userData = F("User/All");
    if(!$userData) {
        $tmp = D("User")->select();
        foreach($tmp as $v) {
            unset($v["password"]);
            $userData[$v["id"]] = $v;
        }
        F("User/All", $userData);
    }
    return $userData;
}

function getCurrentUid() {
    return $_SESSION["user"]["id"];
}

function toDate($str, $format="Y-m-d H:i:s") {
    return date($format, $str);
}

function toUsername($uid) {
    if(!$uid) {
        return L("Unnamed");
    }
    $userData = getUserCache();
    return $userData[$uid]["username"];
}

function toTruename($uid) {
    if(!$uid) {
        return "";
    }
    $userData = getUserCache();
    return $userData[$uid]["truename"];
}

function getPwd($source) {
    return md5($source);
}

function getFactoryCodeAll($params) {
    list($fcid, $colorid, $standardid) = $params;
    return implode("-", $params);
}

function extraFactoryCodeAll($factory_code_all) {
    list($fcid, $colorid, $standardid) = explode("-", $factory_code_all);
    $data = array(
        "factory_code" => $fcid,
        "color_id" => $colorid,
        "standard_id" => $standardid
    );
    return $data;
//    print_r($data);exit;
}

/**
 * 仓库列表
 */
function getStocks() {
    $stocks = F("Stock/List");
    if(!$stocks) {
        $model = D("Stock");
        $stocks = $model->select();
        $stocks = $model->getIndexArray($stocks);
        F("Stock/List", $stocks);
    }
    return $stocks;
}

function toStockName($id) {
    $stocks = getStocks();
    return $stocks[$id];
}

function checkStoreNum($factory_code_all, $num) {
    $model = D("StockProductList");
    $data = $model->where("factory_code_all='".$factory_code_all."'")->find();
    return $data["num"] >= $num;
}

/**
 * 生成日期序列
 */
function makeDateRange($start, $end, $step, $format="m-d") {
    $tmp = range($start, $end, $step);
//    echo "<pre>";
//    echo $start;
//    echo "<br />";
//    echo $end;
//    echo "<br />";
//    echo $step;
//    var_dump($tmp);
    foreach($tmp as $v) {
        $dateRange[] = date($format, $v);
    }
    return $dateRange;
}

function ksortHaveNoIndex ($data) {
    foreach($data as $v) {
        $tmp[] = $v;
    }
    return $tmp;
}

function array_sort($arr,$keys,$type='asc'){ 
	$keysvalue = $new_array = array();
	foreach ($arr as $k=>$v){
		$keysvalue[$k] = $v[$keys];
	}
	if($type == 'asc'){
		asort($keysvalue);
	}else{
		arsort($keysvalue);
	}
	reset($keysvalue);
	foreach ($keysvalue as $k=>$v){
		$new_array[$k] = $arr[$k];
	}
	return $new_array; 
} 

?>
